package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.RealTimeForumComment;
import com.wolves.career.service.RealTimeForumCommentService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * 论坛模块
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/forum")
@CrossOrigin
public class RealTimeForumCommentController {
    @Autowired
    private RealTimeForumCommentService commentService;

    /**
     * 评论
     *
     * @param comment 评论类
     * @return
     */
    @PostMapping("/doComment")
    public R doComment(@RequestBody RealTimeForumComment comment) {
        commentService.save(comment);
        return R.ok().data("success", true);
    }

    //todo 删除评论

    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @DeleteMapping("/deleteComment/{commentId}")
    public R deleteComment(@PathVariable String commentId) {
        commentService.removeById(commentId);
        return R.ok().data("success", true);
    }

    //todo 管理员查看评论

    /**
     * 管理员分页查询所有评论
     *
     * @param comment 评论类
     * @param pageNo  当前页码
     * @param limit   每页限制数量
     * @return
     */
    @PostMapping("/listComment/{pageNo}/{limit}")
    public R listComment(
            @PathVariable long pageNo,
            @PathVariable long limit,
            @RequestBody(required = false) RealTimeForumComment comment) {
        QueryWrapper<RealTimeForumComment> queryWrapper = commentService.getCommentQueryWrapper(comment);
        Page<RealTimeForumComment> page = new Page<>(pageNo, limit);
        IPage<RealTimeForumComment> commentIPage = commentService.page(page, queryWrapper);
        return R.ok().data("records", commentIPage.getRecords()).data("total", commentIPage);
    }
}

